
#include <windows.h>
#include <stdio.h>
#include <string.h>

char *join_string(char *buffer, size_t size, const char **arr, int count, char delimiter)
{
	char *p = buffer;
	int i = 0, len;
	while (i < count) {
		len = strlen(arr[i]);
		if (size - (p - buffer) > len + 3) {
			if (p != buffer)
				*p++ = delimiter;
			*p++ = '"';
			strcpy(p, arr[i]);
			p += len;
			*p++ = '"';
		}
		else {
			break;
		}
		i++;
	}
	*p = '\0';
	return buffer;
}

int main(int argc, const char **argv)
{
	int i = 0;
	char buffer[4096];

	STARTUPINFO si;
    PROCESS_INFORMATION pi;

    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );

	if (argc > 1) {
		join_string(buffer, sizeof(buffer), argv+1, argc-1, ' ');
		if (strlen(buffer) > 0) {
			// Start the child process. 
			if( !CreateProcess( NULL,   // No module name (use command line)
				buffer,         // Command line
				NULL,           // Process handle not inheritable
				NULL,           // Thread handle not inheritable
				FALSE,          // Set handle inheritance to FALSE
				0,              // No creation flags
				NULL,           // Use parent's environment block
				NULL,           // Use parent's starting directory 
				&si,            // Pointer to STARTUPINFO structure
				&pi )           // Pointer to PROCESS_INFORMATION structure
			) 
			{
				LPVOID lpMsgBuf;
				FormatMessage( 
					FORMAT_MESSAGE_ALLOCATE_BUFFER | 
					FORMAT_MESSAGE_FROM_SYSTEM | 
					FORMAT_MESSAGE_IGNORE_INSERTS,
					NULL,
					GetLastError(),
					0, // Default language
					(LPTSTR) &lpMsgBuf,
					0,
					NULL 
				);
				printf( "CreateProcess failed (%d): %s\n", GetLastError(), lpMsgBuf);
				return;
			}
		}
	}
}
